package pers.cz.jefconfig.reader.yaml;

import org.yaml.snakeyaml.Yaml;
import pers.cz.jefconfig.reader.Reader;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @program: JefConfig
 * @description: yaml文件解析
 * yaml语法规则：
 * 1、使用空格做为缩进
 * 2、不以行为单位：
 * @author: Cheng Zhi
 * @create: 2023-04-10 10:00
 **/
public class YamlReader implements Reader {
    @Override
    public void readLine(FileInputStream fileInputStream, Map<String, Object> map) throws IOException {

        Yaml yaml = new Yaml();
        //map.putAll(yaml.load(fileInputStream));
        Map<String, Object>  loadResult = yaml.load(fileInputStream);

        mapHandle(loadResult, map, new StringBuilder());

    }

    private void mapHandle(Map<String, Object> source, Map<String, Object> target, StringBuilder stringBuilder) {
        StringBuilder sb = new StringBuilder();
        sb.append(stringBuilder);
        for (String key : source.keySet()) {
            sb.append(key);
            if (source.get(key) instanceof Map) {
                // 判断一下下一层是否为还为Map，如果不是，则这个一定是一个对象，不需要再次递归
                if (nextIsMap((Map<String, Object>) source.get(key))) {
                    sb.append(".");
                    mapHandle((Map<String, Object>) source.get(key), target, sb);
                } else {
                    target.put(sb.toString(), source.get(key));
                }
            } else {
                target.put(sb.toString(), source.get(key));
            }
            sb = new StringBuilder();
            sb.append(stringBuilder);
        }
    }

    private boolean nextIsMap(Map<String, Object> map) {
        boolean flag = false;
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() instanceof HashMap) {
                flag = true;
            }
        }

        return flag;
    }
    /*
    *//**
     * 自定义的方式实现yaml解析
     * @param fileInputStream
     * @param map
     * @throws IOException
     *//*
    @Override
    public void readLine(FileInputStream fileInputStream, Map<String, String> map) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream));
        List<String> parents = new LinkedList<>();
        String line;
        while (((line = br.readLine()) != null)) {
            if (line.trim().isEmpty()) {
                continue;
            }
            // 注释行
            if (line.trim().startsWith("#")) {
                continue;
            }
            String[] parts = line.split(":", 2);

            String name = parts[0].trim();

            int level = 0;
            while (line.charAt(level) == ' ') {
                level = level + 2;
            }
            level = level / 2;

            while (parents.size() <= level) {
                parents.add("");
            }
            if (level == 0) {
                name = "." + name;
            } else {
                name = parents.get(level - 1) + "." + name;
            }
            String value = parts[1].trim();
            if (!value.isEmpty()) {
                // 去除name的第一个.
                map.put(name.substring(1,name.length()), value);
            }
            parents.add(level, name);
        }

    }*/
}
